package rss_CODE;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.EntityManager;

import rss_POJO.*;

/**
 *
 * @author RSS-team
 */
public class DataBase {

    private final RadioStationSystem rss;
    private final EntityManager em;

    public DataBase(RadioStationSystem rss) {
        this.rss = rss;
        this.em = rss.getEM();
    }

    public List<Artist> readArtistCollection() {
        try {
            // dynamic jpql query για τη δημιουργία λίστας όλων των καλλιτεχνών
            return em.createQuery("SELECT a FROM Artist a ", Artist.class).getResultList();
        } catch(Exception ex) {
            // δεν επιστρέφω τίποτα διότι υπήρξε γενικότερο σφάλμα
            rss.warningBox(null, "σφάλμα κατά την ανάγνωση ARTISTs");
            return java.util.Collections.emptyList();
        } 
    }

    private List<MusicGroup> readMusicGroupCollection() {
        try {
            // dynamic jpql query για τη δημιουργία λίστας όλων των συγκροτημάτων
            return em.createQuery("SELECT mg FROM MusicGroup mg ", MusicGroup.class).getResultList();
        } catch(Exception ex) {
            // δεν επιστρέφω τίποτα διότι υπήρξε γενικότερο σφάλμα
            rss.warningBox(null, "σφάλμα κατά την ανάγνωση MUSICGROUPs");
            return null;
        } 
    }

    private List<Album> readAlbumCollection() {
        try {
            // dynamic jpql query για τη δημιουργία λίστας όλων των άλμπουμ
            return em.createQuery("SELECT a FROM Album a ", Album.class).getResultList();
        } catch(Exception ex) {
            // δεν επιστρέφω τίποτα διότι υπήρξε γενικότερο σφάλμα
            rss.warningBox(null, "σφάλμα κατά την ανάγνωση ALBUMs");
            return null;
        } 
    }
    
    private List<Song> readSongCollection() {
        try {
            // dynamic jpql query για τη δημιουργία λίστας όλων των άλμπουμ
            return em.createQuery("SELECT s FROM Song s ", Song.class).getResultList();
        } catch(Exception ex) {
            // δεν επιστρέφω τίποτα διότι υπήρξε γενικότερο σφάλμα
            rss.warningBox(null, "σφάλμα κατά την ανάγνωση SONGs");
            return null;
        } 
    }

    private List<PlayList> readPlayListCollection() {
        try {
            // dynamic jpql query για τη δημιουργία λίστας όλων των λιστών με τραγούδια
            return em.createQuery("SELECT pl FROM PlayList pl ", PlayList.class).getResultList();
        } catch(Exception ex) {
            // δεν επιστρέφω τίποτα διότι υπήρξε γενικότερο σφάλμα
            rss.warningBox(null, "σφάλμα κατά την ανάγνωση PLAYLISTs");
            return null;
        } 
    }
    
    /* υπόδειγμα σάρωσης, χρήσιμο για XML */
    public void TestXML() {

        System.out.println("");
        for(PlayList pl : readPlayListCollection()) {
            //rss.accessDB(pl,"R");
            System.out.println(pl.getName());
            for(Song s : pl.getSongCollection()) {
                //rss.accessDB(s,"R");
                System.out.println("\t" + s.getTitle());
                System.out.println("\t" + s.getDuration());
                System.out.println("\t" + s.getTracknr());
                System.out.println("\t" + s.getAlbumId().getTitle());
                System.out.println("\t\t" + s.getAlbumId().getReleasedate());
                System.out.println("\t\t" + s.getAlbumId().getType());
                if(s.getAlbumId().getArtistId() != null) System.out.println("\t\t" + s.getAlbumId().getArtistId());
                else if(s.getAlbumId().getMusicgroupId() != null) {
                    System.out.println("\t\t" + s.getAlbumId().getMusicgroupId().getName());
                    for(Artist a : s.getAlbumId().getMusicgroupId().getArtistCollection()) {
                        //rss.accessDB(a,"R");
                        System.out.println("\t\t\t" + a.getArtisticname());
                    }
                }
            }
        }
       
    }

    /* τυπικό γέμισμα της ΒΔ */
    public void FillTheDataBase() {

        //Fill Genres
        MusicGenre mg0 = new MusicGenre("Fun"); rss.accessDB(null, mg0,"W");
        MusicGenre mg1 = new MusicGenre("Rock"); rss.accessDB(null, mg1,"W");
        MusicGenre mg2 = new MusicGenre("Disco"); rss.accessDB(null, mg2,"W");
        MusicGenre mg3 = new MusicGenre("Pop"); rss.accessDB(null, mg3,"W");
        MusicGenre mg4 = new MusicGenre("Latin"); rss.accessDB(null, mg4,"W");
        MusicGenre mg5 = new MusicGenre("Classic"); rss.accessDB(null, mg5,"W");
        MusicGenre mg6 = new MusicGenre("Έντεχνο"); rss.accessDB(null, mg6,"W");
        MusicGenre mg7 = new MusicGenre("Λαϊκά"); rss.accessDB(null, mg7,"W");
        
        //Fill MusicProductionCompanies
        MusicProductionCompany mpc0 = new MusicProductionCompany("Capitol Records, Inc.","1750 N. Vine Street, Hollywood, California, CA 90028","(323) 462-6252"); rss.accessDB(null, mpc0,"W");
        MusicProductionCompany mpc1 = new MusicProductionCompany("Warner Music Group","1633 Broadway, New York, NY 10019","(212) 275-2000"); rss.accessDB(null, mpc1,"W");
        MusicProductionCompany mpc2 = new MusicProductionCompany("ATCO Records","1290 Avenue of the Americas, New York, NY 10104", "(800) 546-3670 "); rss.accessDB(null, mpc2,"W");
        MusicProductionCompany mpc3 = new MusicProductionCompany("Frank Farian Corporation","",""); rss.accessDB(null, mpc3,"W");
        MusicProductionCompany mpc4 = new MusicProductionCompany(" Minos - EMI Α.Ε.","Μεσογείων 245","30 21 06792500"); rss.accessDB(null, mpc4,"W");

        //Fill Artists
        Artist ar0 = new Artist("Richard","Wright","Rick Wright",1,rss.validDate("28/07/1943","dd/MM/yyyy"),"Middlesex - England");
            ar0.setMusicgenreId(mg1); rss.accessDB(null, ar0,"W");
        Artist ar1 = new Artist("David","Gilmour","David Gilmour",1,rss.validDate("06/03/1946","dd/MM/yyyy"),"Cambridge - England");
            ar1.setMusicgenreId(mg1); rss.accessDB(null, ar1,"W");        
        Artist ar2 = new Artist("Nick","Mason","Nick Mason",1,rss.validDate("27/01/1944","dd/MM/yyyy"),"Birmingham - England");
            ar2.setMusicgenreId(mg1); rss.accessDB(null, ar2,"W");   
        Artist ar3 = new Artist("Roger","Waters","Roger Waters",1,rss.validDate("06/09/1943","dd/MM/yyyy"),"Surrey - England");
            ar3.setMusicgenreId(mg1); rss.accessDB(null, ar3,"W");   
        Artist ar4 = new Artist("Syd","Barrett","Syd Barrett",1,rss.validDate("06/01/1946","dd/MM/yyyy"),"Surrey - England");
            ar4.setMusicgenreId(mg1); rss.accessDB(null, ar4,"W");              
            
        Artist ar5 = new Artist("James Patrick","Page","Jimmy Page",1,rss.validDate("09/01/1944","dd/MM/yyyy"),"Middlesex - England");
            ar5.setMusicgenreId(mg1); rss.accessDB(null, ar5,"W");
        Artist ar6 = new Artist("John Paul","Baldwin","John Baldwin",1,rss.validDate("03/01/1946","dd/MM/yyyy"),"Kent - England");
            ar6.setMusicgenreId(mg1); rss.accessDB(null, ar6,"W");        
        Artist ar7 = new Artist("John Henry","Bonham","John Bonham",1,rss.validDate("31/05/1948","dd/MM/yyyy"),"Worcestershire - England");
            ar7.setMusicgenreId(mg1); rss.accessDB(null, ar7,"W");   
        Artist ar8 = new Artist("Robert Anthony","Plant","Robert Plant",1,rss.validDate("20/08/1948","dd/MM/yyyy"),"Staffordshire - England");
            ar8.setMusicgenreId(mg1); rss.accessDB(null, ar8,"W");              

        Artist ar9 = new Artist("Μάνος","Χατζιδάκις","Μάνος Χατζιδάκις",1,rss.validDate("23/10/1925","dd/MM/yyyy"),"Ξάνθη - Ελλάδα");
            ar9.setMusicgenreId(mg5); rss.accessDB(null, ar9,"W");
        Artist ar10 = new Artist("Brian Corrigan");
            ar10.setMusicgenreId(mg1); rss.accessDB(null, ar10,"W");        
        Artist ar11 = new Artist("Michael","Kamen","Michael Kamen",1,rss.validDate("15/04/1948","dd/MM/yyyy"),"New York - United States");
            ar11.setMusicgenreId(mg1); rss.accessDB(null, ar11,"W");   
        Artist ar12 = new Artist("Martin","Fulterman","Mark Snow",1,rss.validDate("26/08/1946","dd/MM/yyyy"),"New York - United States");
            ar12.setMusicgenreId(mg1); rss.accessDB(null, ar12,"W");   
        Artist ar13 = new Artist("Dorian","Rudnytsky","Dorian Rudnytsky",1,rss.validDate("09/07/1944","dd/MM/yyyy"),"New York - United States");
            ar13.setMusicgenreId(mg1); rss.accessDB(null, ar13,"W");              
        Artist ar14 = new Artist("Clifton Νivison");
            ar14.setMusicgenreId(mg1); rss.accessDB(null, ar14,"W");       
            
        Artist ar15 = new Artist("Liz","Mitchell","Liz Mitchell",0,rss.validDate("12/07/1952","dd/MM/yyyy"),"Clarendon, JAMAICA");
            ar15.setMusicgenreId(mg2); rss.accessDB(null, ar15,"W");
        Artist ar16 = new Artist("Bobby","Farrell","Bobby Farrell",1,rss.validDate("06/10/1949","dd/MM/yyyy"),"Aruba, WEST INDIES");
            ar16.setMusicgenreId(mg2); rss.accessDB(null, ar16,"W");
        Artist ar17 = new Artist("Marcia","Barrett","Marcia Barrett",0,rss.validDate("14/10/1948","dd/MM/yyyy"),"St.Catherines, JAMAICA");
            ar17.setMusicgenreId(mg2); rss.accessDB(null, ar17,"W");
        Artist ar18 = new Artist("Maizie","Williams","Maizie Williams",0,rss.validDate("25/03/1951","dd/MM/yyyy"),"Monserrat, WEST INDIES");
            ar18.setMusicgenreId(mg2); rss.accessDB(null, ar18,"W");

        Artist ar19 = new Artist("Χάρης","Κατσιμίχας","Χάρης Κατσιμίχας",1,rss.validDate("19/10/1952","dd/MM/yyyy"),"Αθήνα");
            ar19.setMusicgenreId(mg6); rss.accessDB(null, ar19,"W");
        Artist ar20 = new Artist("Πάνος","Κατσιμίχας","Πάνος Κατσιμίχας",1,rss.validDate("19/10/1952","dd/MM/yyyy"),"Αθήνα");
            ar20.setMusicgenreId(mg6); rss.accessDB(null, ar20,"W");


        //Fill MusicGroups    
        MusicGroup gr0 = new MusicGroup("Pink Floyd",rss.validDate("23/03/1965","dd/MM/yyyy"));
            List<Artist> gr0Artists = new ArrayList<>();
            gr0Artists.add(ar0); gr0Artists.add(ar1); gr0Artists.add(ar2); gr0Artists.add(ar3); gr0Artists.add(ar4);
            gr0.setArtistCollection(gr0Artists);
            rss.accessDB(null, gr0,"W");
            
        MusicGroup gr1 = new MusicGroup("Led Zeppelin",rss.validDate("26/12/1968","dd/MM/yyyy"));
            List<Artist> gr1Artists = new ArrayList<>();
            gr1Artists.add(ar5); gr1Artists.add(ar6); gr1Artists.add(ar7); gr1Artists.add(ar8);
            gr1.setArtistCollection(gr1Artists);
            rss.accessDB(null, gr1,"W");
            
        MusicGroup gr2 = new MusicGroup("Manos Hadjidakis / New York Rock & Roll Ensemble",rss.validDate("30/01/1970","dd/MM/yyyy"));
            List<Artist> gr2Artists = new ArrayList<>();
            gr2Artists.add(ar9); gr2Artists.add(ar10); gr2Artists.add(ar11); gr2Artists.add(ar12); gr2Artists.add(ar13); gr2Artists.add(ar14);
            gr2.setArtistCollection(gr2Artists);
            rss.accessDB(null, gr2,"W");  

        MusicGroup gr3 = new MusicGroup("Boney-M",rss.validDate("01/01/1976","dd/MM/yyyy"));
            List<Artist> gr3Artists = new ArrayList<>();
            gr3Artists.add(ar15); gr3Artists.add(ar16); gr3Artists.add(ar17); gr3Artists.add(ar18); gr3.setArtistCollection(gr3Artists);
            rss.accessDB(null, gr3,"W");

        MusicGroup gr4 = new MusicGroup("Κατσιμίχας Χάρης & Πάνος",rss.validDate("01/01/1982","dd/MM/yyyy"));
            List<Artist> gr4Artists = new ArrayList<>();
            gr4Artists.add(ar19);
            gr4Artists.add(ar20);
            gr4.setArtistCollection(gr4Artists);
            rss.accessDB(null, gr4,"W");
        
        //Fill Albums
        Album a0 = new Album("The Dark Side Of The Moon",rss.validDate("24/03/1973","dd/MM/yyyy"),"CD",1);
            a0.setMpcompanyId(mpc0);
            a0.setMusicgroupId(gr0);
            rss.accessDB(null, a0,"W");

        Album a1 = new Album("Celebration Day",rss.validDate("16/11/2012","dd/MM/yyyy"),"LP",2);
            a1.setMpcompanyId(mpc1);
            a1.setMusicgroupId(gr1);
            rss.accessDB(null, a1,"W");            

        Album a2 = new Album("Reflections",rss.validDate("30/01/1970","dd/MM/yyyy"),"CD",1);
            a2.setMpcompanyId(mpc2);
            a2.setArtistId(ar9);
            rss.accessDB(null, a2,"W");              

        Album a3 = new Album("Night Flight to Venus",rss.validDate("01/01/1978","dd/MM/yyyy"),"LP",1);
            a3.setMpcompanyId(mpc3);
            a3.setMusicgroupId(gr3);
            rss.accessDB(null, a3,"W");

        Album a4 = new Album("Ζεστά Ποτά ",rss.validDate("01/01/1985","dd/MM/yyyy"),"LP",1);
            a4.setMpcompanyId(mpc4);
            a4.setMusicgroupId(gr4);
            rss.accessDB(null, a4,"W");

        //Fill Songs
        Song s0 = new Song("Speak To Me",90,1);
            s0.setAlbumId(a0); rss.accessDB(null, s0,"W");
        Song s1 = new Song("Breathe",163,2);
            s1.setAlbumId(a0); rss.accessDB(null, s1,"W");
        Song s2 = new Song("On The Run",210,3);
            s2.setAlbumId(a0); rss.accessDB(null, s2,"W");
        Song s3 = new Song("Time",353,4);
            s3.setAlbumId(a0); rss.accessDB(null, s3,"W");
        Song s4 = new Song("The Great Gig In The Sky",255,5);
            s4.setAlbumId(a0); rss.accessDB(null, s4,"W");
        Song s5 = new Song("Money",330,6);
            s5.setAlbumId(a0); rss.accessDB(null, s5,"W");
        Song s6 = new Song("Us And Them",394,7);
            s6.setAlbumId(a0); rss.accessDB(null, s6,"W");
        Song s7 = new Song("Any Colour You Like",214,8);
            s7.setAlbumId(a0); rss.accessDB(null, s7,"W");
        Song s8 = new Song("Brain Damage",230,9);
            s8.setAlbumId(a0); rss.accessDB(null, s8,"W");
        Song s9 = new Song("Eclipse",105,10);
            s9.setAlbumId(a0); rss.accessDB(null, s9,"W");              
            
        Song s10 = new Song("Good Times Bad Times",210,1);
            s10.setAlbumId(a1); rss.accessDB(null, s10,"W");
        Song s11 = new Song("Ramble On",337,2);
            s11.setAlbumId(a1); rss.accessDB(null, s11,"W");
        Song s12 = new Song("Black Dog",318,3);
            s12.setAlbumId(a1); rss.accessDB(null, s12,"W");
        Song s13 = new Song("In My Time Of Dying",661,4);
            s13.setAlbumId(a1); rss.accessDB(null, s13,"W");
        Song s14 = new Song("For Your Life",368,5);
            s14.setAlbumId(a1); rss.accessDB(null, s14,"W");
        Song s15 = new Song("Trampled Under Foot",362,6);
            s15.setAlbumId(a1); rss.accessDB(null, s15,"W");
        Song s16 = new Song("Nobody's Fault But Mine",384,7);
            s16.setAlbumId(a1); rss.accessDB(null, s16,"W");
        Song s17 = new Song("No Quarter",540,8);
            s17.setAlbumId(a1); rss.accessDB(null, s17,"W");
        Song s18 = new Song("Since I've Been Loving You",755,9);
            s18.setAlbumId(a1); rss.accessDB(null, s18,"W");
        Song s19 = new Song("Dazed And Confused",679,10);
            s19.setAlbumId(a1); rss.accessDB(null, s19,"W"); 
        Song s20 = new Song("Stairway To Heaven",508,11);
            s20.setAlbumId(a1); rss.accessDB(null, s20,"W");
        Song s21 = new Song("The Song Remains The Same",335,12);
            s21.setAlbumId(a1); rss.accessDB(null, s21,"W");
        Song s22 = new Song("Misty Mountain Hop",508,13);
            s22.setAlbumId(a1); rss.accessDB(null, s22,"W");
        Song s23 = new Song("Kashmir",528,14);
            s23.setAlbumId(a1); rss.accessDB(null, s23,"W");
        Song s24 = new Song("Whole Lotta Love",409,15);
            s24.setAlbumId(a1); rss.accessDB(null, s24,"W");
        Song s25 = new Song("Rock And Roll",259,16);
            s25.setAlbumId(a1); rss.accessDB(null, s25,"W");
           
        Song s26 = new Song("Orpheus",151,1);
            s26.setAlbumId(a2); rss.accessDB(null, s26,"W");
        Song s27 = new Song("The Day",220,2);
            s27.setAlbumId(a2); rss.accessDB(null, s27,"W");
        Song s28 = new Song("Love Her",257,3);
            s28.setAlbumId(a2); rss.accessDB(null, s28,"W");
        Song s29 = new Song("Dance Of The Dogs",178,4);
            s29.setAlbumId(a2); rss.accessDB(null, s29,"W");
        Song s30 = new Song("Kemal",241,5);
            s30.setAlbumId(a2); rss.accessDB(null, s30,"W");
        Song s31 = new Song("Dedication",167,6);
            s31.setAlbumId(a2); rss.accessDB(null, s31,"W");
        Song s32 = new Song("The Three Answers",228,7);
            s32.setAlbumId(a2); rss.accessDB(null, s32,"W");
        Song s33 = new Song("Street Song",206,8);
            s33.setAlbumId(a2); rss.accessDB(null, s33,"W");
        Song s34 = new Song("Bitter Way",255,9);
            s34.setAlbumId(a2); rss.accessDB(null, s34,"W");
        Song s35 = new Song("Noble Dame",191,10);
            s35.setAlbumId(a2); rss.accessDB(null, s35,"W");              

        Song s36 = new Song("Nightflight to Venus",306,1);
            s36.setAlbumId(a3); rss.accessDB(null, s36,"W");
        Song s37 = new Song("Rasputin",351,2);
            s37.setAlbumId(a3); rss.accessDB(null, s37,"W");
        Song s38 = new Song("Painter Man",190,3);
            s38.setAlbumId(a3); rss.accessDB(null, s38,"W");
        Song s39 = new Song("He Was a Steppenwolf",411,4);
            s39.setAlbumId(a3); rss.accessDB(null, s39,"W");
        Song s40 = new Song("King of the Road",156,5);
            s40.setAlbumId(a3); rss.accessDB(null, s40,"W");
        Song s41 = new Song("Rivers of Babylon",258,6);
            s41.setAlbumId(a3); rss.accessDB(null, s41,"W");
        Song s42 = new Song("Voodoonight",211,7);
            s42.setAlbumId(a3); rss.accessDB(null, s42,"W");
        Song s43 = new Song("Brown Girl in the Ring",242,8);
            s43.setAlbumId(a3); rss.accessDB(null, s43,"W");
        Song s44 = new Song("Never Change Lovers in the Middle of the Night",332,9);
            s44.setAlbumId(a3); rss.accessDB(null, s44,"W");
        Song s45 = new Song("Heart of Gold",240,10);
            s45.setAlbumId(a3); rss.accessDB(null, s45,"W");

        Song s46 = new Song("Παίξε Βραχνή Μου Φυσαρμόνικα",120,1);
            s46.setAlbumId(a4); rss.accessDB(null, s46,"W");
        Song s47 = new Song("Προσωπικές Οπτασίες",164,2);
            s47.setAlbumId(a4); rss.accessDB(null, s47,"W");
        Song s48 = new Song("Φάνης",346,3);
            s48.setAlbumId(a4); rss.accessDB(null, s48,"W");
        Song s49 = new Song("Τις Κυριακές Από Παιδί Τις Σιχαινόμουνα",312,4);
            s49.setAlbumId(a4); rss.accessDB(null, s49,"W");
        Song s50 = new Song("Μια Βραδιά Στο Λούκι",193,5);
            s50.setAlbumId(a4); rss.accessDB(null, s50,"W");
        Song s51 = new Song("Ρίτα - Ριτάκι",194,6);
            s51.setAlbumId(a4); rss.accessDB(null, s51,"W");
        Song s52 = new Song("Για Ένα Κομμάτι Ψωμί",228,7);
            s52.setAlbumId(a4); rss.accessDB(null, s52,"W");
        Song s53 = new Song("Υπόγειο",252,8);
            s53.setAlbumId(a4); rss.accessDB(null, s53,"W");
        Song s54 = new Song("Κορίτσια Της Συγνώμης",252,9);
            s54.setAlbumId(a4); rss.accessDB(null, s54,"W");
        Song s55 = new Song("Γέλα Πουλί Μου",184,10);
            s55.setAlbumId(a4); rss.accessDB(null, s55,"W");
            
        //Fill Playlists
        PlayList pl0 = new PlayList("All Songs", new Date());
            rss.accessDB(null, pl0,"W");
            
        PlayList pl1 = new PlayList("My Favorite Songs", new Date());
            Collection<Song> pl1Songs = new ArrayList<>();
            pl1Songs.add(s0); pl1Songs.add(s10); pl1Songs.add(s26); pl1Songs.add(s37); pl1Songs.add(s41); pl1Songs.add(s50);
            pl1.setSongCollection(pl1Songs);
            rss.accessDB(null, pl1,"W");   
    }
    
    //* τυπική εκτύπωση της ΒΔ */
    public void PrintTheDataBase() {

        System.out.println("");
        for(Artist a : readArtistCollection()) {
            rss.accessDB(null, a,"R");
            System.out.print(a.getArtisticname());
            System.out.print(a.getAlbumCollection());
            System.out.println(a.getMusicGroupCollection());
        }
        System.out.println("");
        for(MusicGroup mg : readMusicGroupCollection()) {
            rss.accessDB(null, mg,"R");
            System.out.print(mg.getName());
            System.out.print(mg.getAlbumCollection());
            System.out.println(mg.getArtistCollection());
        }
        System.out.println("");
        for(Album a : readAlbumCollection()) {
            rss.accessDB(null, a,"R");
            System.out.print(a.getTitle());
            System.out.println(a.getSongCollection());
        }
        System.out.println("");
        for(Song s : readSongCollection()) {
            rss.accessDB(null, s,"R");
            System.out.print(s.getTitle());
            System.out.println(s.getPlayListCollection());
        }
        System.out.println("");
        for(PlayList pl : readPlayListCollection()) {
            rss.accessDB(null, pl,"R");
            System.out.print(pl.getName());
            System.out.println(pl.getSongCollection());
        }
        
    }
}
